#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include <string.h>

LinkedList *create_doublelist()
{//	1、创建一个管理结点
	LinkedList *l = (LinkedList *)malloc(sizeof(*l));
    //	2、对管理结点进行初始操作(此时为空链表)
    l->first = NULL;	//	指向单链表的第一个结点(空链表)
    l->last = NULL;		//	指向单链表的最后一个结点(空链表)
    l->nodenum = 0;		//	链表的结点数量
    //	3、管理结点返回
    return l;
}
// 按序插入
void insert_head_doublelist(LinkedList* l, int num,ElemType name,ElemType writer) 
{
    if(l)
    {   
        
        Node* newNode = (Node*)malloc(sizeof(Node));

        newNode->num = num;
        newNode->name = (ElemType)malloc(strlen(name) + 1);///!!!首先是给这个指针类型的data申请空间，空间根据输入的字长加1《\0》
        strcpy(newNode->name, name);//!!!!然后将这个data的值赋给newNode->data，这里是字符串所以只能用strcpy函数
        newNode->writer = (ElemType)malloc(strlen(writer) + 1);///!!!首先是给这个指针类型的data申请空间，空间根据输入的字长加1《\0》
        strcpy(newNode->writer, writer);//!!!!然后将这个data的值赋给newNode->data，这里是字符串所以只能用strcpy函数
        newNode->prev = NULL;
        newNode->next = NULL;
        
        if(l->nodenum==0)
        {
            l->first=l->last=newNode;
        }
        else
        {   
            Node* p =l->first;
            while(p!=NULL)
            {
                if(p->num>num)
                {
                    break;
                }
                p=p->next;
            }
            //找到了，或者没找到都有可能
            if(p==l->first)
            {
                newNode->next=l->first;
                l->first->prev=newNode;
                l->first=newNode;
            }
             else if(p==NULL)//尾插
            {
                newNode->prev=l->last;
                l->last->next=newNode;
                l->last=newNode;
            }
            else
            {
                //查到中间
                p->prev->next=newNode;
                newNode->prev=p->prev;
                p->prev=newNode;
                newNode->next=p;
            }

        }
        l->nodenum++;
    }
}


void printList(LinkedList* l)
{
    if (!l || !l->first) 
    {
        printf("链表为空\n");
        return;
    }
    Node* p=l->first;
    while(p)
    {
        printf("%d ",p->num);
        printf("%s ",p->name);
        printf("%s ",p->writer);
        printf("\n");
        p=p->next;
    }
    printf("\n");
}

Node* findElement(LinkedList* l, ElemType name) //通过书名定位
{
    if (!l || !l->first) 
    {
        return NULL;
    }
    Node* p=l->first;
    while(p)
    {
        if(strcmp(p->name,name)==0)
        {
            return p;
        }
        p=p->next;
    }
    return NULL;
}

Node* findElement1(LinkedList* l, ElemType writer) //通过书名定位
{
    if (!l || !l->first) 
    {
        return NULL;
    }
    Node* p1=l->first;
    while(p1)
    {
       if(strcmp(p1->writer,writer)==0)
        {
            return p1;
        }
        p1=p1->next; 
    }
    return NULL;
}

    

void modifyElement1(LinkedList* l, ElemType oldData, ElemType newData)
{
    if (!l || !l->first) 
    {
        return ;
    }
    Node*p =findElement(l,oldData);
    if(!p)
    {
        return;
    }
    free(p->name);
    p->name=(ElemType)malloc(strlen(newData)+1);
    strcpy(p->name, newData);
    free(p->writer);
    p->writer=(ElemType)malloc(strlen(newData)+1);
    strcpy(p->writer, newData);
}

void modifyElement2(LinkedList* l, ElemType oldData, ElemType newData,ElemType newData1)
{
    if (!l || !l->first) 
    {
        return ;
    }
    Node*p =findElement(l,oldData);
    if(!p)
    {
        return;
    }
    free(p->name);
    p->name=(ElemType)malloc(strlen(newData)+1);
    strcpy(p->name, newData);
    free(p->writer);
    p->writer=(ElemType)malloc(strlen(newData)+1);
    strcpy(p->writer, newData1);
}

void modifyElement3(LinkedList* l, ElemType oldData, ElemType newData)
{
    if (!l || !l->first) 
    {
        return ;
    }
    Node*p =findElement(l,oldData);
    if(!p)
    {
        return;
    }
    free(p->name);
    p->name=(ElemType)malloc(strlen(newData)+1);
    strcpy(p->name, newData);
}

void modifyElement4(LinkedList* l, ElemType oldData, ElemType newData)
{
    if (!l || !l->first) 
    {
        return ;
    }
    Node*p =findElement1(l,oldData);
    if(!p)
    {
        return;
    }
    free(p->writer);
    p->writer=(ElemType)malloc(strlen(newData)+1);
    strcpy(p->writer, newData);
}

void deleteElement(LinkedList* l, ElemType name)
{
    if (!l || !l->first) 
    {
        return ;
    }
    Node*p =findElement(l,name);
    if(!p)
    {
        return;
    }
    if(l->nodenum==1)
    {
        l->first=l->last=NULL;
    }
    else if(p==l->first)
    {
        l->first=p->next;
        p->prev=NULL;
    }
    else if(p==l->last)
    {
        l->last=p->prev;
        l->last->next=NULL;

    }
    else
    {
        p->prev->next=p->next;
        p->next->prev=p->prev;
    }
    free(p->name);
    free(p->writer);

    free(p);
    l->nodenum--;
}


void freeList(LinkedList* list) {
    if (!list) return;
    
    Node* current = list->first;
    while (current) {
        Node* p = current->next;
        free(current->name);
        free(current->writer);
        free(current);
        current = p;
    }
    
    list->first = NULL;
    list->last = NULL;
    list->nodenum = 0;
}


void destroyList(LinkedList** listPtr) 
{
    if (!listPtr || !*listPtr) return;
    
    freeList(*listPtr);
    free(*listPtr);
    *listPtr = NULL;
}